package org.dreamwork.jasmine2.parser;

import java.io.IOException;
import java.io.Reader;
import java.io.FileReader;

public class Parser {
    private boolean intag = false, inscript = false, recordtag = false, recordattr = false;
    private StringBuffer buff = new StringBuffer ();
    private String currentTag;
    private char lastchar = '\u0000';

    public void parse (String file) throws IOException {
        Reader reader = new FileReader (file);
        int n;

        while ((n = reader.read ()) != -1) {
            lastchar = buff.length () > 0 ? buff.charAt (buff.length () - 1) : '\u0000';
            char ch = (char) n;
            switch (ch) {
                case '<' :
                    if (!intag && !inscript) { // 不在tag或脚本中，设置tag标志
                        intag     = true;
                        recordtag = true;
                        if (buff.toString ().trim ().length () > 0) {
                            System.out.println ("Content {" + buff.toString ().trim () + "}");
                            buff = new StringBuffer ();
                        }
                    } else if (currentTag.equalsIgnoreCase ("script")) {
                        if (buff.toString ().trim ().length () > 0) {
                            System.out.println ("Content {" + buff.toString ().trim () + "}");
                            buff = new StringBuffer ();
                        }
                        inscript = false;
                    } else {
                        buff.append (ch);
                    }
                    break;
                case '>' :
                    if (intag) {
                        if (recordattr) { // 有属性的标签
                            System.out.println ("Attribute [" + buff.toString ().trim () + "]");
                        } else { // 无属性的标签
                            recordtag = false;
                            process_tag (buff.toString ());
                        }
                        recordattr = false;
                        intag =      false;
                    }
                    buff = new StringBuffer ();
                    break;
                case '\t':
                    if (lastchar == '@') process_space (ch);
                    break;
                case ' ' :
                    process_space (ch);
                    break;
                default  :
                    buff.append (ch);
                    break;
            }
        }
    }

    private void process_space (char ch) {
        if (!intag) { // 不在tag中
            buff.append (ch);
        } else { // 在tag中
            // 如果上个字符是'%'，并且正在记录tag名称，为JSPDirective，忽略该空格
            if (lastchar == '@' && recordtag) return ;
            if (lastchar != '@' && recordtag) { // 在tag中，并且上个字符不是'@', 代表着tag记录结束
                recordtag = false;
                recordattr = true;
                process_tag (buff.toString ());
                buff = new StringBuffer ();
            } else if (recordattr) {
                buff.append (ch);
            }
        }
    }

    private void process_tag (String tagName) {
        tagName = tagName.toLowerCase ().trim ();
        if (tagName.equalsIgnoreCase ("script"))
            inscript = true;
        else if (tagName.equalsIgnoreCase ("/script"))
            inscript = false;
        currentTag = tagName;
        System.out.println ("Tag (" + tagName + ")");
    }

    public static void main (String[] args) throws Exception {
        new Parser ().parse ("d:/test.html");
    }
}
